package dataStructures;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

import org.apache.hadoop.io.WritableComparable;

public class TwoVectorsFirstIntSecondDouble implements WritableComparable<TwoVectorsFirstIntSecondDouble> {

        private int[] vector1;
        private double[] vector2;

        public TwoVectorsFirstIntSecondDouble() {
                super();
        }
        
        public TwoVectorsFirstIntSecondDouble(int [] v1, double [] v2) {
            super();
            vector1 = v1;
            vector2 = v2;
        }


        public TwoVectorsFirstIntSecondDouble(int[] intArray,
				Ratings[] accumulateCbElementsValues) {
			vector1 = intArray;
			vector2 = new double[accumulateCbElementsValues.length];
			for(int i = 0 ; i < vector2.length ; i++){
				if(accumulateCbElementsValues[i].getRatingsCounter() != 0 )
					vector2[i] = ((double)accumulateCbElementsValues[i].getRatingsSum()) / ((double)accumulateCbElementsValues[i].getRatingsCounter());
				else 
					vector2[i] = 0;
						}  
		}

		@Override
        public void write(DataOutput out) throws IOException {
                out.writeInt(vector1.length);
                for (int i = 0; i < vector1.length; i++)
                        out.writeInt(vector1[i]);
                out.writeInt(vector2.length);
                for (int i = 0; i < vector2.length; i++)
                        out.writeDouble(vector2[i]);
        }

        @Override
        public void readFields(DataInput in) throws IOException {
                String line = in.readLine();
                StringTokenizer st = new StringTokenizer(line , "\t\n");
        		String a = st.nextToken();
        		String b = st.nextToken();
                ArrayList<Integer> vectorA = new ArrayList<Integer>();
                ArrayList<Integer> vectorB = new ArrayList<Integer>();
        		
        		st = new StringTokenizer(a , "[], \t\n");
                while(st.hasMoreTokens()){
                	vectorA.add(Integer.parseInt(st.nextToken()));
                }
                
                vector1 = new int[vectorA.size()] ; 
                for(int i = 0 ; i < vector1.length ; i++){
                	vector1[i] = vectorA.get(i).intValue();
                }
                
                st = new StringTokenizer(b , "[], \t\n");
                while(st.hasMoreTokens()){
                	vectorB.add(Integer.parseInt(st.nextToken()));
                }
                
                vector2 = new double[vectorB.size()] ; 
                for(int i = 0 ; i < vector2.length ; i++){
                	vector2[i] = vectorB.get(i).doubleValue();
                }
                
                
        }

        @Override
        public int compareTo(TwoVectorsFirstIntSecondDouble o) {

                boolean equals = true;
                for (int i = 0; i < vector1.length; i++) {
                        if (vector1[i] != o.vector1[i]) {
                                equals = false;
                                break;
                        }
                }
                
                for (int i = 0; i < vector2.length; i++) {
                    if (vector2[i] != o.vector2[i]) {
                            equals = false;
                            break;
                    }
            }
                
                if (equals)
                        return 0;
                else
                        return 1;
        }

        public int[] getVector1() {
                return vector1;
        }
        
        public double[] getVector2() {
            return vector2;
    }

        

        @Override
        public String toString() {
                return   Arrays.toString(vector1) + "\t" + Arrays.toString(vector2);
        }

}